#Boa:Frame:FUEliminar

import wx
import psycopg2
import sys
import pprint
import os
import commands

def create(parent):
    return FUEliminar(parent)

[wxID_FUELIMINAR, wxID_FUELIMINARBELIMINAR, wxID_FUELIMINARBMENU, 
 wxID_FUELIMINARCOD, wxID_FUELIMINARPANEL1, wxID_FUELIMINARTEXTCODIG, 
] = [wx.NewId() for _init_ctrls in range(6)]

def delete_usuario(cod):
  os.system("rm -r -f huella/%s"%(cod))
  conn_string = "host='localhost' port='5432' dbname='DB778' user='postgres' password='123456789'" 
  conn = psycopg2.connect(conn_string)
  cursor = conn.cursor()
  consulta = "DELETE from catedratico where codigo_catedratico = "+cod
  cursor.execute(consulta)
  conn.commit()
  cursor.close()
  conn.close()

class FUEliminar(wx.Frame):
    codigo=''
    Codigo_catedratico=0
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Frame.__init__(self, id=wxID_FUELIMINAR, name=u'FUEliminar',
              parent=prnt, pos=wx.Point(506, 167), size=wx.Size(300, 220),
              style=wx.DEFAULT_FRAME_STYLE, title=u'Eliminar Usuario')
        self.SetClientSize(wx.Size(300, 220))

        self.panel1 = wx.Panel(id=wxID_FUELIMINARPANEL1, name='panel1',
              parent=self, pos=wx.Point(0, 0), size=wx.Size(300, 220),
              style=wx.TAB_TRAVERSAL)

        self.bmenu = wx.Button(id=wxID_FUELIMINARBMENU, label=u'Menu',
              name=u'bmenu', parent=self.panel1, pos=wx.Point(40, 168),
              size=wx.Size(105, 40), style=0)
        self.bmenu.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD, False,
              u'Sans'))
        self.bmenu.Bind(wx.EVT_BUTTON, self.OnBmenuButton,
              id=wxID_FUELIMINARBMENU)

        self.beliminar = wx.Button(id=wxID_FUELIMINARBELIMINAR,
              label=u'Eliminar', name=u'beliminar', parent=self.panel1,
              pos=wx.Point(185, 105), size=wx.Size(105, 40), style=0)
        self.beliminar.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD, False,
              u'Sans'))
        self.beliminar.Bind(wx.EVT_BUTTON, self.OnBeliminarButton,
              id=wxID_FUELIMINARBELIMINAR)

        self.Cod = wx.StaticText(id=wxID_FUELIMINARCOD, label=u'Codigo',
              name=u'Cod', parent=self.panel1, pos=wx.Point(24, 54),
              size=wx.Size(66, 18), style=0)
        self.Cod.SetFont(wx.Font(11, wx.SWISS, wx.NORMAL, wx.BOLD, False,
              u'Sans'))

        self.textCodig = wx.TextCtrl(id=wxID_FUELIMINARTEXTCODIG,
              name=u'textCodig', parent=self.panel1, pos=wx.Point(105, 48),
              size=wx.Size(180, 30), style=0, value=u'')
        self.textCodig.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.NORMAL,
              False, u'Sans'))

    def __init__(self, parent,cod):
        global codigo
        global Codigo_catedratico
        codigo=cod
        Codigo_catedratico=cod
        self._init_ctrls(parent)

    def OnBmenuButton(self, event):
        self.Close(True)
        event.Skip()

    def OnBeliminarButton(self, event):
	cant=0
        conn_string = "host='localhost' port='5432' dbname='DB778' user='postgres' password='123456789'" 
        conn = psycopg2.connect(conn_string)
      	cursor = conn.cursor()
      	consulta = "SELECT COUNT(*) from catedratico where codigo_catedratico = "+self.textCodig.GetValue()
      	cursor.execute(consulta)
      	res=cursor.fetchone()
      	cant=int(res[0])
      	conn.commit()
      	cursor.close()
      	conn.close()
      	if (cant==1):
		delete_usuario(self.textCodig.GetValue ())
      		dlg = wx.MessageDialog(self, 'Usuario eliminado','',wx.OK | wx.ICON_INFORMATION)
      		dlg.ShowModal()
      		dlg.Destroy()
	else:
		dlg1 = wx.MessageDialog(self, 'No existe','',wx.OK | wx.ICON_INFORMATION)
      		dlg1.ShowModal()
      		dlg1.Destroy()
		

	event.Skip()
